package com.sg.service.base.component.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.component.req.*;
import com.sg.entity.OmsConfScheme;
import com.sg.service.base.component.MOmsConfSchemeService;
import com.wicket.okrcomponent.common.dto.FieldInfo;
import com.wicket.okrcomponent.common.exception.BizException;
import com.wicket.okrcomponent.dal.mapper.mbg.OmsConfSchemeMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsConfSchemeServiceImpl
        extends ServiceImpl<OmsConfSchemeMapper, OmsConfScheme>
        implements MOmsConfSchemeService {

    @Autowired
    OmsConfSchemeMapper omsConfSchemeMapper;

    /**
     * 2-3-04查询配置方案详情[1923]
     * gen by moon at 4/22/2023, 9:05:32 PM
     */
    @Trace(operationName = "2-3-04查询配置方案详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsConfScheme queryConfSchemeDetail(QueryConfSchemeDetailReq reqDto) {

        try {
            reqDto.setIsValid("FALSE");
            QueryWrapper<OmsConfScheme> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsConfScheme> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsConfScheme retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-04批量查询配置方案列表[2093]
     * gen by moon at 10/16/2022, 11:03:58 AM
     */
    @Trace(operationName = "2-3-04批量查询配置方案列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsConfScheme> batchQueryConfSchemeList(BatchQueryConfSchemeListReq reqDto) {

        try {
            QueryWrapper<OmsConfScheme> queryWrapper = new QueryWrapper<>();

            if (reqDto.getConfSchemeList() == null || reqDto.getConfSchemeList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("conf_scheme_id", reqDto.getConfSchemeList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsConfScheme.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsConfScheme.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsConfScheme.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsConfScheme>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-04查询配置方案列表[1924]
     * gen by moon at 10/16/2022, 11:03:57 AM
     */
    @Trace(operationName = "2-3-04查询配置方案列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsConfScheme> queryConfSchemeList(QueryConfSchemeListReq reqDto) {

        try {
            QueryWrapper<OmsConfScheme> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsConfScheme.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsConfScheme.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsConfScheme.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsConfScheme>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-04删除配置方案[1922]
     * gen by moon at 10/16/2022, 11:03:56 AM
     */
    @Trace(operationName = "2-3-04删除配置方案")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteConfScheme(DeleteConfSchemeReq reqDto) {
        reqDto.setSubjectLifeCycle("EDITING");
        try {
            Map<String, Object> columnMap = CommonFunctionHelper.objectToMap(reqDto);
            return super.removeByMap(columnMap);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-3-04修改配置方案[1921]
     * gen by moon at 10/16/2022, 11:03:55 AM
     */
    @Trace(operationName = "2-3-04修改配置方案")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateConfScheme(OmsConfScheme omsConfScheme) {
        omsConfScheme.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsConfScheme != null && omsConfScheme.getSubjectLifeCycle() != null && omsConfScheme.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfScheme.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsConfScheme != null && omsConfScheme.getSubjectLifeCycle() != null && omsConfScheme.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsConfScheme.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsConfScheme.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsConfScheme != null && omsConfScheme.getSubjectLifeCycle() != null && omsConfScheme.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfScheme.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsConfScheme != null && omsConfScheme.getSubjectLifeCycle() != null && omsConfScheme.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsConfScheme.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsConfScheme);
    }

    /**
     * 2-3-04新增配置方案[1920]
     * gen by moon at 10/16/2022, 11:03:55 AM
     */
    @Trace(operationName = "2-3-04新增配置方案")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addConfScheme(OmsConfScheme omsConfScheme) {
        omsConfScheme.setConfSchemeId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsConfScheme.setSubjectLifeCycle("EDITING");
        omsConfScheme.setIsArchive("FALSE");
        omsConfScheme.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsConfScheme.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsConfScheme != null && omsConfScheme.getSubjectLifeCycle() != null && omsConfScheme.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfScheme.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsConfScheme.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsConfScheme.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsConfScheme != null && omsConfScheme.getSubjectLifeCycle() != null && omsConfScheme.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsConfScheme.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsConfScheme.setIsValid("FALSE");
        omsConfScheme.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsConfScheme.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsConfScheme)) {
            return omsConfScheme.getConfSchemeId();
        } else {
            return "-1";
        }

    }

    /**
     * 2-3-06批量查询配置方案列表ByCodes[4824]
     * gen by moon at 10/16/2022, 11:03:59 AM
     */
    @Trace(operationName = "2-3-06批量查询配置方案列表ByCodes")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsConfScheme> batchQueryConfSchemeListByCodes(BatchQueryConfSchemeListByCodesReq reqDto) {

        try {
            QueryWrapper<OmsConfScheme> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getConfSchemeList() == null || reqDto.getConfSchemeList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("conf_scheme_code", reqDto.getConfSchemeList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsConfScheme.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsConfScheme.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsConfScheme.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);

            return ((List<OmsConfScheme>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * M2查询配置方案测试详情[6487]
     * gen by moon at 5/6/2023, 2:54:22 PM
     */
    @Trace(operationName = "M2查询配置方案测试详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsConfScheme queryConfSchemeTestDetail(OmsConfScheme omsConfScheme) {

        try {

            QueryWrapper<OmsConfScheme> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(omsConfScheme);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsConfScheme> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsConfScheme retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
